//
//  locationViewController.m
//  MoneyLog
//
//  Created by Max on 11-5-4.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "locationViewController.h"


@implementation locationViewController
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceLabel;

@synthesize indicatorView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [latitudeLabel release];
	[longitudeLabel release];
	[horizontalAccuracyLabel release];
	[altitudeLabel release];
	[verticalAccuracyLabel release];
	[distanceLabel release];
	[indicatorView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //create location manager
	locationManager = [[CLLocationManager alloc]init];
	locationManager.delegate = self;
	locationManager.desiredAccuracy = kCLLocationAccuracyBest;
	[locationManager startUpdatingLocation];
	
	[indicatorView startAnimating];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark locationManager delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
	[indicatorView stopAnimating];
    
	if(!originalLocation)
	{
		originalLocation = newLocation;
	}
    
    NSLog(@"latitude = %f",newLocation.coordinate.latitude);
    NSLog(@"longitude = %f",newLocation.coordinate.longitude);
	
	NSString *latitudeString = [[NSString alloc]initWithFormat:@"%g°",newLocation.coordinate.latitude];
	[latitudeLabel setText:latitudeString];
	[latitudeString release];
	
	NSString *longitudeString = [[NSString alloc]initWithFormat:@"%g°",newLocation.coordinate.longitude];
	[longitudeLabel setText:longitudeString];
	[longitudeString release];
	
	NSString *horizontalAccuracyString = [[NSString alloc]initWithFormat:@"%g m",newLocation.horizontalAccuracy];
	[horizontalAccuracyLabel setText:horizontalAccuracyString];
	[horizontalAccuracyString release];
	
	NSString *altitudeString = [[NSString alloc]initWithFormat:@"%g°",newLocation.altitude];
	[altitudeLabel setText:altitudeString];
	[altitudeString release];
	
	NSString *verticalAccuracyString = [[NSString alloc]initWithFormat:@"%g m°",newLocation.verticalAccuracy];
	[verticalAccuracyLabel setText:verticalAccuracyString];
	[verticalAccuracyString release];
	
	CLLocationDistance distance = [newLocation distanceFromLocation:originalLocation];
	
	NSString *distanceString = [[NSString alloc]initWithFormat:@"%g m",distance];
	[distanceLabel setText:distanceString];
	[distanceString release];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
	[indicatorView stopAnimating];
    
	NSString *errorString;
	
	if([error code] == kCLErrorDenied)
	{
		errorString = @"Access Not Allowed";
	}
	else if([error code] == kCLErrorLocationUnknown)
	{
		errorString = @"Unknown Error";
	}
	
	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Cannot get location" 
												   message:errorString
												  delegate:nil
										 cancelButtonTitle:@"Close"
										 otherButtonTitles:nil];
	[alert show];
	[alert release];
}

-(IBAction)OK
{
    [self dismissModalViewControllerAnimated:YES];
}


@end
